#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from pydantic import BaseModel, ConfigDict, Field
from typing import List, Optional

class CpuInfo(BaseModel):
    model_config = ConfigDict(populate_by_name=True)

    cpu_num: Optional[int]  = Field(None, alias='cpuNum', description='CPU 核心数')
    used: Optional[float]   = Field(None, alias='used', description='CPU 用户使用率')
    sys: Optional[float]    = Field(None, alias='sys', description='CPU 系统使用率')
    free: Optional[float]   = Field(None, alias='free', description='CPU 当前空闲率')


class MemoryInfo(BaseModel):
    model_config = ConfigDict(populate_by_name=True)

    total: Optional[str]     = Field(None, alias='total', description='内存总量')
    used: Optional[str]      = Field(None, alias='used', description='已用内存')
    free: Optional[str]      = Field(None, alias='free', description='剩余内存')
    usage: Optional[float]   = Field(None, alias='usage', description='内存使用率')


class SystemInfo(BaseModel):
    model_config = ConfigDict(populate_by_name=True)

    server_ip: Optional[str]    = Field(None, alias='serverIp', description='服务器 IP')
    server_name: Optional[str]  = Field(None, alias='serverName', description='服务器名称')
    os_arch: Optional[str]      = Field(None, alias='osArch', description='系统架构')
    os_name: Optional[str]      = Field(None, alias='osName', description='操作系统')
    user_dir: Optional[str]     = Field(None, alias='userDir', description='项目路径')


class PythonInfo(MemoryInfo):
    model_config = ConfigDict(populate_by_name=True)

    name: Optional[str]         = Field(None, alias='name', description='Python 名称')
    version: Optional[str]      = Field(None, alias='version', description='Python 版本')
    start_time: Optional[str]   = Field(None, alias='startTime', description='启动时间')
    run_time: Optional[str]     = Field(None, alias='runTime', description='运行时长')
    home: Optional[str]         = Field(None, alias='home', description='安装路径')


class SystemFiles(BaseModel):
    
    model_config = ConfigDict(populate_by_name=True)
    
    drive_letter_path: Optional[str]= Field(None, alias='driveLetterPath', description='盘符路径')
    drive_letter_type: Optional[str]= Field(None, alias='driveLetterType', description='盘符类型')
    file_type: Optional[str]        = Field(None, alias='fileType', description='文件类型')
    total: Optional[str]            = Field(None, alias='total', description='总大小')
    used: Optional[str]             = Field(None, alias='used', description='已经使用量')
    free: Optional[str]             = Field(None, alias='free', description='剩余大小')
    usage: Optional[str]            = Field(None, alias='usage', description='资源的使用率')


class ServerMonitorInfo(BaseModel):
    """服务监控信息
    """
    cpu: Optional[CpuInfo]          = Field(None, alias='cpu', description='CPU 相关信息')
    py: Optional[PythonInfo]        = Field(None, alias='py', description='Python 相关信息')
    memory: Optional[MemoryInfo]    = Field(None, alias='memory', description='內存相关信息')
    system: Optional[SystemInfo]    = Field(None, alias='system', description='服务器相关信息')
    system_files: Optional[List[SystemFiles]]  = Field([], alias='systemFiles', description='磁盘相关信息')
    
    model_config = ConfigDict(populate_by_name=True)
    
    def to_dict(self):
        data_dict = self.model_dump(by_alias=True, exclude=['system_files', 'systemFiles'])
        system_file_list = [obj.model_dump(by_alias=True) for obj in self.system_files]
        data_dict['systemFiles'] = system_file_list
        return data_dict
